﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	public class Curve extends Sprite {
		private var tw:Number = 800;
		private var th:Number = 100;
		private var xy_color:Number = 0x999999;
		private var dl_color:Number = 0x008000;
		//左右边距
		private var sideW:Number = 30;
		//底边距
		private var sideH:Number = 20;
		//数据
		private var time_start:String;
		private var time_end:String;
		private var lList:Array;
		private var sList:Array;
		private var cList:Array;
		//
		public function Curve() {
		}
		private function clearChart():void {
			while (numChildren) {
				removeChildAt(0);
			}
		}
		public function set xywh(arr:Array):void {
			x = arr[0];
			y = arr[1];
			tw = arr[2];
			th = arr[3];
		}
		public function show(_time_start:String, _time_end:String, _lList:Array, _sList:Array, _cList:Array):void {
			time_start = _time_start;
			time_end = _time_end;
			lList = _lList;
			sList = _sList;
			cList = _cList;
			//
			clearChart();
			drawChart();
		}
		private function drawChart():void {
			//画X轴
			draw_x_line();
			//画Y轴
			draw_y_line();
			//画曲线数据
			draw_d_line();
		}
		private function draw_x_line():void {
			var x_line:Sprite = new Sprite();
			x_line.graphics.lineStyle(1, xy_color);
			x_line.graphics.drawRect(sideW, 0, tw - sideW*2, th - sideH);
			addChild(x_line);
			//添加时间标识
			x_line.addChild(getLabel(time_start, sideW, th - sideH, "left", "top"));
			x_line.addChild(getLabel(time_end, tw - sideW, th - sideH, "right", "top"));
		}
		private function draw_y_line():void {
			var y_line:Sprite = new Sprite();
			addChild(y_line);
			//添加时间标识
			var l:int = sList.length;
			var h:Number = (th - sideH)/l;
			y_line.graphics.lineStyle(1, xy_color, 0.2);
			for (var i:int = 0; i< l; i++) {
				var ly:Number = th - sideH - h*i;
				y_line.addChild(getLabel(sList[i], sideW - 2, ly, "right", "bottom"));
				y_line.addChild(getLabel(sList[i], tw - sideW, ly, "left", "bottom"));
				if (i) {
					y_line.graphics.moveTo(sideW, ly);
					y_line.graphics.lineTo(tw - sideW, ly);
				}
			}
		}
		private function draw_d_line():void {
			var d_line:Sprite = new Sprite();
			d_line.graphics.lineStyle(1, dl_color);
			d_line.graphics.beginFill(dl_color, 0.2);
			var l:int = lList.length;
			if (l > 1) {
				var d:Number = (tw - sideW*2)/(l - 1);
				var h:Number = th - sideH;
				var ah:Number = parseInt(sList[sList.length - 1]) - parseInt(sList[0]);
				for (var i:int = 0; i<l; i++) {
					var tx:Number = Math.round(sideW + d*i);
					var ty:Number = h - h*(parseInt(lList[i]) - parseInt(sList[0]))/ah;
					//trace(tx + "|" +ty);
					if (i) {
						d_line.graphics.lineTo(tx, ty);
					} else {
						d_line.graphics.moveTo(tx, ty);
					}
				}
				d_line.graphics.lineStyle(1, dl_color, 0);
				d_line.graphics.lineTo(tw - sideW, th - sideH);
				d_line.graphics.lineTo(sideW, th - sideH);
				d_line.graphics.endFill();
				addChild(d_line);
			}
		}
		//
		private function getLabel(s:String, tx:int, ty:int, align:String = "center", valign:String = "middle"):TextField {
			var label:TextField = new TextField();
			label.selectable = false;
			label.mouseEnabled = false;
			label.autoSize = "left";
			label.htmlText = s;
			if (align == "left") {
				label.x = tx;
			} else if (align == "right") {
				label.x = tx - label.textWidth;
			} else {
				label.x = tx - label.textWidth/2;
			}
			if (valign == "top") {
				label.y = ty;
			} else if (valign == "bottom") {
				label.y = ty - label.textHeight;
			} else {
				label.y = ty - label.textHeight/2;
			}
			return label;
		}
	}
}